<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of report
 *
 * @author Константин Охотник
 */

class Storage extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->issetSession('Storage');
        $this->load->model('StorageModel');
        $list_category = $this->StorageModel->Category();
        $list_fabricator = $this->StorageModel->Fabricator();
        $this->crud->assign('category',$list_category)
                ->assign('fabricator',$list_fabricator);
    }
    
    public function index()
    {
        $content_array = $this->StorageModel->getProduct(base_url()."storage/index/",PER_PAGE_CRM);
        $pages = $this->pagination->create_links();
        $this->crud->assign('storage' ,$content_array)
                ->assign('pages', $pages)->display('storage.tpl');
    }

    public function add()
    {
       die( $this->StorageModel->Add($this->input->post()));
    }
    
    public function upload()
    {
        $config['upload_path'] = $_SERVER['DOCUMENT_ROOT']."/application/uploads";
        $config['allowed_types'] = "jpg|jpeg|png|gif|flv|mp4|wmv|doc|docx|xsl|xslx|ppt|pptx|zip|rar|tar";
        $config['max_size']	= 2048;
        $config['max_width'] = 2000;
        $config['max_height'] =2000;
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);

        if ($this->upload->do_upload() == false)
        {
            $error = array('error' => $this->upload->display_errors());
            echo json_encode($error);
        }
        else
        {
            $data = $this->upload->data();
            $name = date('dmy_Hisu');
            $arr_name = explode('.',$data['file_name']);
            if(isset($arr_name[count($arr_name)-1]))
            {
                rename($data['full_path'], $_SERVER['DOCUMENT_ROOT']."/static/images/".$name.$data['file_ext']);
                $full_name = "/static/images/".$name.$data['file_ext'];
            }

            die(  json_encode($full_name));
        }
    }
    
    public function find()
    {
        $r = $this->input->get();
        if(($r['id_category']=="" && $r['id_fabricator']=="")||!isset($r))
        {
            redirect(site_url("storage/"), 'refresh');
        }
        $content_array= $this->StorageModel->Filter(base_url()."storage/find/",$this->input->get(),PER_PAGE_CRM);
        $pages = $this->pagination->create_links();

        if( isset($content_array['error']))
        {
            $this->crud->assign('error' ,$content_array)->display('storage.tpl'); 
        }
        else
        {
            $this->crud->assign('storage' ,$content_array)
                ->assign('pages', $pages)->display('storage.tpl');
          
        }
    }
    
    public function rmpicture()
    {
        $p = $this->input->post();
        die(unlink($_SERVER['DOCUMENT_ROOT'].$p['path_img']));
    }
    
    public function delete($id)
    {
        $this->StorageModel->delete_post('tb_product', 'id', $id);
        redirect(site_url("storage/"), 'refresh');
    }
    
}